﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    class WordsCounter
    {
        /// <summary>
        /// 统计字数
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static int CountWords(string text)
        {
            int chWords = 0;
            int enWords = 0;
            if (string.IsNullOrEmpty(text))
                return 0;
            text = text.Trim();
            chWords = Regex.Matches(text, @"[^\u0000-\u007f,\s]").Count;
            foreach (Match m in Regex.Matches(text, @"[\u0000-\u007f]*"))
            {
                string tmpstr = m.Value.Trim();
                if (!string.IsNullOrEmpty(tmpstr))
                {
                    tmpstr = string.Format("\t{0}\t", tmpstr);
                    enWords += Regex.Matches(tmpstr, "\\s+").Count - 1;
                }
            }
            return chWords + enWords;
        }

    }
}
